import java.io.*;
import java.util.*;

public class MultiTest {

    final static int NUM_CALCULATIONS = (Integer.MAX_VALUE/4); //Integer.MAX_VALUE;

    // private long singleTime;
    // private long douime;
    // private long tripleTime;
    // private long quadTime;

    // long startTime;
    // long endTime;

    // public static boolean isPrime(int number) {
    // 	for(int i=2;i<n;i++) {
    // 	    if(n%i==0)1000
    // 		return false;
    // 	}
    // 	return true;
    // }
	
    // private long runSingleTest() {
    // 	startTime = System.currentTimeMillis();
    // 	for (int i = 0; i < NUMCALCULATIONS; i++) {
    // 	    isPrime(i);
    // 	}
    // 	endTime = System.currentTimeMillis();
    // 	return; = endTime - startTime;
    // }

    // private long runDoubleTest() {
    // 	int set1 = NUMCALCULATIONS/2;
    // 	startTime = System.currentTimeMillis();
    // 	for (int i = 0; i < set1; i++) {
    // 	    isPrime(i);
    // 	}
    // 	endTime = System.currentTimeMillis();
    // 	return; = endTime - startTime;
    // }

    public void test1Thread(){
	PrimeThread thread1 = new PrimeThread(1, 1, NUM_CALCULATIONS);
	thread1.setPriority(Thread.MAX_PRIORITY);
	thread1.start();
    }

    public void test2Threads(){
	PrimeThread thread1 = new PrimeThread(2, 1, NUM_CALCULATIONS/2);
	PrimeThread thread2 = new PrimeThread(2, NUM_CALCULATIONS/2+1, NUM_CALCULATIONS);
	thread1.setPriority(Thread.MAX_PRIORITY);
	thread2.setPriority(Thread.MAX_PRIORITY);
	thread1.start();
	thread2.start();
    }

    public void test3Threads(){
	PrimeThread thread1 = new PrimeThread(3 ,1, NUM_CALCULATIONS/3);
	PrimeThread thread2 = new PrimeThread(3, NUM_CALCULATIONS/3+1, (NUM_CALCULATIONS/3)*2);
	PrimeThread thread3 = new PrimeThread(3,(NUM_CALCULATIONS/3)*2+1, NUM_CALCULATIONS);
	thread1.setPriority(Thread.MAX_PRIORITY);
	thread2.setPriority(Thread.MAX_PRIORITY);
	thread3.setPriority(Thread.MAX_PRIORITY);
	thread1.start();
	thread2.start();
	thread3.start();
    }

    public void test4Threads(){
	PrimeThread thread1 = new PrimeThread(4, 1, NUM_CALCULATIONS/4);
	PrimeThread thread2 = new PrimeThread(4, NUM_CALCULATIONS/4+1, NUM_CALCULATIONS/2);
	PrimeThread thread3 = new PrimeThread(4, NUM_CALCULATIONS/2+1, (NUM_CALCULATIONS/4)*3);
	PrimeThread thread4 = new PrimeThread(4, (NUM_CALCULATIONS/4)*3+1, NUM_CALCULATIONS);
	thread1.setPriority(Thread.MAX_PRIORITY);
	thread2.setPriority(Thread.MAX_PRIORITY);
	thread3.setPriority(Thread.MAX_PRIORITY);
	thread4.setPriority(Thread.MAX_PRIORITY);
	thread1.start();
	thread2.start();
	thread3.start();
	thread4.start();
    }

    public static void main(String[] args) {
	//String s = "";
	MultiTest driver = new MultiTest();


	System.out.println("Running test with " + NUM_CALCULATIONS + " calculations with 1 cores.... ");
	//singleTime = runSingleTest();
	driver.test1Thread();
	System.out.println("Running test with " + NUM_CALCULATIONS + " calculations with 2 cores.... ");
	//doubleTime = runDoubleTest();
	driver.test2Threads();
	System.out.println("Running test with " + NUM_CALCULATIONS + " calculations with 3 cores.... ");
	//tripleTime = run TripleTest();
	driver.test3Threads();
	System.out.println("Running test with " + NUM_CALCULATIONS + " calculations with 4 cores.... ");
	//quadTime = runQuadTest();
	driver.test4Threads();

	// s ="Results:\n";
	// s+=("\n");
	// s+="Runtime with 1 thread: " + singleTime + (" milliseconds\n");
	// s+="Runtime with 2 thread: " + doubleTime + (" milliseconds\n");
	// s+="Runtime with 3 thread: " + tripleTime + (" milliseconds\n");
	// s+="Runtime with 4 thread: " + quadTime + (" milliseconds\n");

	// System.out.println(s);
    }

    public class PrimeThread extends Thread {
	int startInt;
	int endInt;

	long startTime;
	long endTime;

	int numThreads;

	PrimeThread(int numThreads, int startInt, int endInt) {
	    this.numThreads = numThreads;
	    this.startInt = startInt;
	    this.endInt = endInt;
	}

	public boolean isPrime(int number) {
	    for(int i=2;i<number;i++) {
		if(number%i==0)
		    return false;
	    }
	    return true;
	}

	public void run() {
	    startTime = System.currentTimeMillis();
	    for (int i = startInt; i < endInt; i++) {
		isPrime(i);
	    }
	    endTime = System.currentTimeMillis();
	    System.out.println("" + numThreads + "thread test: Tested from " + startInt + " to " + endInt + " in " + (endTime-startTime) + " milliseconds.");
	}
    }
}
